﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
using System.Windows.Threading;
using BrainTechLLC;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.WPF;
using ImageAppDataModels;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace ShowMultiple.Plugin
{
    public class ShowMultipleImagesWindowHandler : WindowHandlerBase<ShowMultipleImagesWindow, List<BitmapAndDescription>>, IShowMultipleImagesWindowHandler
    {
        public event EventHandler Closed;
        public static PluginDefinitionLoaded<IShowMultipleImagesWindowHandler> Handler;

        public ShowMultipleImagesWindowHandler(Window1 parentWindow, PluginDefinitionLoaded<IShowMultipleImagesWindowHandler> handler)
            : base(parentWindow)
        {
            Handler = handler;
            _parentWindow = parentWindow;
        }

        public override void OpenWindow(List<BitmapAndDescription> items)
        {
            if (_window == null)
            {
                _window = new ShowMultipleImagesWindow() { Owner = _parentWindow };
                _window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                _window.ClosedWindow += new EventHandler<ObjectEventArgs<BitmapsAndDescriptionsViewModel>>(_window_ClosedWindow);
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.AfterFilterBitmap += new EventHandler<ObjectEventArgs<Bitmap, string, string, ISingleFilter>>(Collection_AfterFilterBitmap);
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.BeforeFilterBitmap += new EventHandler<ObjectEventArgs<Bitmap, string, string, ISingleFilter>>(Collection_BeforeFilterBitmap);
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.BeginFilterBitmap += new EventHandler<ObjectEventArgs<Bitmap, string>>(Collection_BeginFilterBitmap);

                _window.HorizontalAlignment = HorizontalAlignment.Stretch;
                _window.VerticalAlignment = VerticalAlignment.Stretch;
                _window.Show();
            }

            _window.ShowActivated = true;
            _window.Show();

            if (_window.IsVisible && Settings.Values.ShowMultipleImagesLocation != null)
            {
                if (Settings.Values.ShowMultipleImagesWindowState == WindowState.Maximized)
                {
                    _window.WindowState = WindowState.Maximized;
                }
                else if (Settings.Values.ShowMultipleImagesWindowState != WindowState.Minimized)
                {
                    if (Settings.Values.ShowMultipleImagesLocation.Width == 0)
                    {
                        _window.WindowState = WindowState.Maximized;
                    }
                    else
                    {
                        _window.Left = Settings.Values.ShowMultipleImagesLocation.Left;
                        _window.Top = Settings.Values.ShowMultipleImagesLocation.Top;
                        _window.Width = Settings.Values.ShowMultipleImagesLocation.Width;
                        _window.Height = Settings.Values.ShowMultipleImagesLocation.Height;
                    }
                }
            }

            BitmapsAndDescriptionsViewModel viewModel = new BitmapsAndDescriptionsViewModel();

            for (int n = 0; n < items.Count; n++)
            {
                viewModel.Add(new BitmapAndDescriptionViewModel(items[n]));
            }

            _window.BitmapsAndDescriptionsViewModels = viewModel;
        }

        void Collection_BeforeFilterBitmap(object sender, ObjectEventArgs<Bitmap, string, string, ISingleFilter> e)
        {
            Bitmap bm = e.obj1.Clone() as Bitmap;
            BitmapAndDescription bmDesc = new BitmapAndDescription() { Description = e.obj2, FileName = e.obj3, OriginalBitmap = bm, Filter = e.obj4 };
            AddImageToCollection(bmDesc);
        }

        protected void RunDispatcherClear()
        {
            if (_window != null)
            {
                _window.BitmapsAndDescriptionsViewModels.Clear();
            }
        }

        protected void AddImageToCollection(BitmapAndDescription bmDesc)
        {
            Application.Current.Dispatcher.RunOnDispatcher(new OneParamDelegate((o) =>
                {
                    BitmapAndDescriptionViewModel vm = o as BitmapAndDescriptionViewModel;

                    if (_window != null)
                    {
                        _window.BitmapsAndDescriptionsViewModels.Add(vm);
                    }
                }),
                DispatcherPriority.Normal, new BitmapAndDescriptionViewModel(bmDesc));
        }

        void Collection_BeginFilterBitmap(object sender, ObjectEventArgs<Bitmap, string> e)
        {
            Application.Current.Dispatcher.RunOnDispatcher(new VoidDelegate(() => RunDispatcherClear()), DispatcherPriority.Normal);
            Bitmap bm = e.obj1.Clone() as Bitmap;
            BitmapAndDescription bmDesc = new BitmapAndDescription() { Bitmap = bm, Description = "Original", Filter = null, OriginalBitmap = bm, FileName = e.obj2 };
            AddImageToCollection(bmDesc);
        }

        void Collection_AfterFilterBitmap(object sender, ObjectEventArgs<Bitmap, string, string, ISingleFilter> e)
        {
            //Bitmap bm = e.obj1.Clone() as Bitmap;
            //BitmapAndDescription bmDesc = new BitmapAndDescription() { Bitmap = bm, Description = e.obj2, FileName = e.obj3, OriginalBitmap = bm };
            //AddImageToCollection(bmDesc);
        }

        void _window_ClosedWindow(object sender, ObjectEventArgs<BitmapsAndDescriptionsViewModel> e)
        {
            if (_window != null)
            {
                Closed.RaiseEvent(this);
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.BeforeFilterBitmap -= Collection_BeforeFilterBitmap;
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.AfterFilterBitmap -= Collection_AfterFilterBitmap;
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Collection.BeginFilterBitmap -= Collection_BeginFilterBitmap;
                _window.ClosedWindow -= _window_ClosedWindow;
                //if (_window.DockablePane != null)
                //{
                //    _window.DockablePane.OnStateChanged -= DockablePane_OnStateChanged;
                //}
                //if (_window.DocumentsPane != null)
                //{
                //    _window.DocumentsPane.DocumentClosing -= DocumentsPane_DocumentClosing;
                //}

                if (_window.BitmapsAndDescriptionsViewModels != null)
                {
                    _window.BitmapsAndDescriptionsViewModels.Clear();
                    _window.BitmapsAndDescriptionsViewModels = null;
                }
            }
            _window = null;
        }
    }
}
